package net.j4love.zookeeper.watch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * @author He Peng
 * @create 2017-05-18 14:40
 * @update 2017-05-18 14:40
 * @see
 */
public class WatcherDemo {


    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.err.println("type : " + event.getType() + "\r\n" +
                                    "path : " + event.getPath() + "\r\n" +
                                    "state : " + event.getState() + "\r\n" +
                                    "wrapper : " + event.getWrapper());

            }
        },false);

        zk.create("/watch","监控回调机制".getBytes("UTF-8"),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.EPHEMERAL);

        zk.setData("/watch" , "《人性的弱点》作者戴尔· 卡耐基".getBytes("UTF-8"), -1);

        zk.delete("/watch" , -1);

        zk.close();
    }
}
